package search.service.manage;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.helper.json.JsonToJavaObject;
import org.elasticsearch.action.get.GetResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import common.config.IndexTypeNames;
import common.dao.elastic.ElasticSearchDao;
import search.service.search.SimpleSearchService_V1;

import javax.inject.Inject;

/**
 * Created by julianzliu on 2016/7/3.
 */
@Component
public class ElasticDocumentService {
    final Logger logger = LoggerFactory.getLogger(SimpleSearchService_V1.class);
    private final ObjectMapper mapper;
    private final ElasticSearchDao elasticsearchDao;


    //@Autowired
    @Inject
    public ElasticDocumentService(
            ElasticSearchDao elasticsearchDao,
            ObjectMapper mapper) {
        this.mapper = mapper;
        this.elasticsearchDao = elasticsearchDao;
    }

    /*****************************************
     *
     * @param oriLevel
     * @param id
     * @return
     */
    public String get(String oriLevel, String id) {
        String level = IndexTypeNames.MediaDB.FileSetName;
        if(oriLevel.equals("program") ) level = IndexTypeNames.MediaDB.ProgramSetName;
        else if(oriLevel.equals("sequence")) level = IndexTypeNames.MediaDB.SequenceSetName;
        else if(oriLevel.equals("scene")) level = IndexTypeNames.MediaDB.SceneSetName;
        else if(oriLevel.equals("shot")) level = IndexTypeNames.MediaDB.ShotSetName;

        GetResponse response = elasticsearchDao.get(level,id);
        logger.debug("get({})= \n{}", id, response.getSourceAsString() );

        String result_str = response.getSourceAsString();
        JsonNode result_node = JsonToJavaObject.loadJsonNode(result_str);
        ((ObjectNode)result_node).remove("Indexdata");

        //return response.getSourceAsString();
        return result_node.toString();
    }


    /********************************************
     *
     * @param oriLevel
     * @param id
     * @return
     */
    public String delete(String oriLevel, String id){
        return oriLevel+"_"+id;
    }
}
